import os
import json
import model
import unittest
import sqlalchemy


class Image(unittest.TestCase):

    engine = sqlalchemy.create_engine('sqlite:///:memory:')
    model.init(engine)
    session = model.DbSession()

    def setUp(self):
        model.metadata.create_all(self.engine)

    def tearDown(self):
        model.metadata.drop_all(self.engine)

    def test_post_json(self):
        data = json.load(open(os.path.join('data', 'post.json')))
        for datum in data:
            post = model.Post(1, 1)
            post.load(datum)

            image = model.Image(post)
            self.session.add(image)

            image.save_tags(post.tags)

        if self.session.new or self.session.dirty:
            self.session.commit()

        total_image = self.session.query(model.Image).count()
        self.assertEquals(total_image, 13, 'Wrong total image.')

        total_tag = self.session.query(model.Tag).count()
        self.assertEquals(total_tag, 22, 'Wrong total tag.')

        total_image_tag = self.session.query(model.ImageTag).count()
        self.assertEquals(total_image_tag, 68, 'Wrong total tag.')
